package test.design_pattern.castle_game.step2;

import java.util.Scanner;

public class Game {
    private Room currentRoom;

    public Game()
    {
        createRooms();
    }

    private void createRooms()
    {
        Room outside, lobby, pub, study, bedroom;

        //	制造房间
        outside = new Room("城堡外");
        lobby = new Room("大堂");
        pub = new Room("小酒吧");
        study = new Room("书房");
        bedroom = new Room("卧室");
        
        //	初始化房间的出口
        outside.setExits(null, lobby, study, pub);
        lobby.setExits(null, null, null, outside);
        pub.setExits(null, outside, null, null);
        study.setExits(outside, bedroom, null, null);
        bedroom.setExits(null, null, null, study);

        currentRoom = outside;  //	从城堡门外开始
    }

    private void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到城堡！");
        System.out.println("这是一个超级无聊的游戏。");
        System.out.println("如果需要帮助，请输入 'help' 。");
        System.out.println();
        showPrompt();
    }

    private void goRoom(String direction)
    {
        Room nextRoom = currentRoom.goRoom(direction);
        if (nextRoom == null) {
            System.out.println("那里没有门！");
        }
        else {
            showPrompt();
        }
    }

    // 以下为用户命令
    private void printHelp()
    {
        System.out.print("迷路了吗？你可以做的命令有：go bye help");
        System.out.println("如：\tgo east");
    }

    /**
     * 消除了代发复制
     */
    public void showPrompt(){
        System.out.println("你在" + this.currentRoom);
        System.out.print("出口有: " + this.currentRoom.getExitDesc());
        System.out.println();
    }
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Game game = new Game();
		game.printWelcome();

        /**
         * 以框架+数据来提高可扩展性
         * 1。命令的解析是否可以脱离if-else
         * 2。新建Handler来处理命令
         * 3。用Hash表来保存命令和Handler之间的关系
         *
         */
        while ( true ) {
        		String line = in.nextLine();
        		String[] words = line.split(" ");
        		if ( words[0].equals("help") ) {
        			game.printHelp();
        		} else if (words[0].equals("go") ) {
        			game.goRoom(words[1]);
        		} else if ( words[0].equals("bye") ) {
        			break;
        		}
        }
        
        System.out.println("感谢您的光临。再见！");
        in.close();
	}

}
